<!-- src/views/Login.vue -->
<template>
  <div class="login-container">
    <div class="login-box">
      <div class="login-title">登 录</div>
      <el-form :model="admin" ref="loginForm" :rules="rules">
        <el-form-item prop="username">
          <el-input
            placeholder="请输入账号"
            prefix-icon="el-icon-user"
            size="medium"
            v-model="admin.username"
          ></el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input
            placeholder="请输入密码"
            show-password
            prefix-icon="el-icon-lock"
            size="medium"
            v-model="admin.password"
          ></el-input>
        </el-form-item>
        <el-form-item>
          <el-button
            style="width: 30%"
            size="medium"
            type="primary"
            @click="login"
            >登 录</el-button
          >
          <el-button
            style="width: 30%"
            size="medium"
            type="default"
            @click="$router.push({ name: 'Register' })"
            >注 册</el-button
          >
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
import { reactive, ref, onMounted } from 'vue';
import { useRouter } from 'vue-router';
import { ElNotification } from 'element-plus';

export default {
  name: 'Login',
  setup() {
    const router = useRouter();
    const loginForm = ref(null);
    const admin = reactive({
      username: '',
      password: '',
    });

    const rules = {
      username: [
        { required: true, message: '请输入用户名', trigger: 'blur' },
      ],
      password: [
        { required: true, message: '请输入密码', trigger: 'blur' },
      ],
    };

    // 页面加载时检查是否已登录（是否有 token）
    onMounted(() => {
      const token = localStorage.getItem('authToken');
      if (token) {
        router.push({ name: 'Dashboard' }); // 如果有 token，直接跳转到 Dashboard
      }
    });

    // 登录逻辑
    const login = () => {
      loginForm.value.validate((valid) => {
        if (valid) {
          if (admin.username === 'admin' && admin.password === 'password') {
            localStorage.setItem('authToken', 'your-auth-token');
            ElNotification.success({
              title: '成功',
              message: '登录成功',
              type: 'success',
            });
            router.push({ name: 'Dashboard' });
          } else {
            ElNotification.error({
              title: '错误',
              message: '用户名或密码错误！',
              type: 'error',
            });
          }
        }
      });
    };

    return {
      admin,
      rules,
      login,
      loginForm,
    };
  },
};
</script>

<style scoped>
.login-container {
  height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  background-color: #f5f5f5;
}

.login-box {
  width: 400px;
  padding: 30px;
  background-color: #ffffff;
  border-radius: 10px;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
}

.login-title {
  margin-bottom: 20px;
  text-align: center;
  font-size: 24px;
  font-weight: bold;
  color: #42b983;
}
</style>
